package com.whacos.modules.system.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.whacos.common.annotations.Log;
import com.whacos.common.controller.BaseController;
import com.whacos.common.entity.Tree;
import com.whacos.common.enums.DelFlagEnum;
import com.whacos.common.shiro.ShiroUtils;
import com.whacos.common.utils.MD5Utils;
import com.whacos.common.utils.ObjectUtils;
import com.whacos.common.utils.StringUtils;
import com.whacos.common.vo.ResponseVO;
import com.whacos.modules.system.entity.File;
import com.whacos.modules.system.entity.SysMenu;
import com.whacos.modules.system.service.FileService;
import com.whacos.modules.system.service.SysMenuService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author xiongdun
 * @description LoginController
 * @since 2018/11/17 17:11
 */
@Controller
@Slf4j
public class LoginController extends BaseController {

    @Autowired
    private SysMenuService sysMenuService;
    @Autowired
    private FileService fileService;

    @GetMapping({"/", ""})
    public String welcome() {
        return "redirect:/app/blog";
    }

    @Log("请求访问主页")
    @GetMapping({"/index"})
    public String index(Model model) {
        List<Tree<SysMenu>> menus = sysMenuService.listSysMenuTreeByUserId(getUserId());
        model.addAttribute("menus", menus);
        model.addAttribute("name", getUser().getRealName());
        // 封装查询Sql
        File file = this.fileService.getOne(Wrappers.lambdaQuery(new File())
                        .eq(File::getDelFlag, DelFlagEnum.NORMAL.getValue())
                        .eq(File::getId, this.getUser().getAvatarId()));
        if (ObjectUtils.isNotEmpty(file) && file.getUrlAddress() != null) {
            if (StringUtils.isNotEmpty(file.getUrlAddress())) {
                model.addAttribute("picUrl", file.getUrlAddress());
            } else {
                model.addAttribute("picUrl", "/img/photo_s.jpg");
            }
        } else {
            model.addAttribute("picUrl", "/img/photo_s.jpg");
        }
        model.addAttribute("username", getUser().getUsername());
        return "index_v1";
    }

    // @Log("跳转到用户登录")
    @GetMapping("/login")
    public String login() {
        return "login";
    }

    @Log("用户登录")
    @PostMapping("/login")
    @ResponseBody
    public ResponseVO ajaxLogin(String username, String password) {
        password = MD5Utils.encrypt(username, password);
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
            return ResponseVO.success();
        } catch (AuthenticationException e) {
            return ResponseVO.error("用户或密码错误");
        }
    }

    // @Log("用户登出")
    @GetMapping("/logout")
    public String logout() {
        ShiroUtils.logout();
        return "redirect:/login";
    }

    // @Log("跳转到主页面")
    @GetMapping("/main")
    public String main() {
        return "main";
    }

}
